/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.BusinessLayer;
using EXtremecode.PresentationLayer;
using EXtremecode.Utils;
using EXtremecode.Common;
using System.Text;
using System.IO;
using EXtremecode.Logging;
using EXtremecode.DataAccessLayer;

namespace EXtremecode.PresentationLayer
{


    /// <summary>
    /// Summary description for ParentPage
    /// </summary>
   
    public class EditParentPage : ParentPage
    {
        private BusinessObjectPaper boPaper;
        private BusinessObject bo;
        public BusinessObjectPaper BOPaper
        {
            get
            {
                if (boPaper == null)
                {
                    if (IsUpdate)
                    {
                        boPaper = GetBOCollectionAfterSettingKey().GetBOPaper();
                    }
                    else
                    {
                        boPaper = editPage.GetBOPaper();
                    }
                }
                return boPaper;

            }
        }

        public BusinessObject BO
        {
            get
            {
                if (bo == null)
                {
                    if (IsUpdate)
                    {
                        bo = GetBOCollectionAfterSettingKey().GetBusinessObject();
                    }
                }
                return bo;

            }
        }
        private IEditPage editPage;

        public EditParentPage()
            : base()
        {
            if (!(this is IEditPage))
            {
                throw new Exception("EditPage should implement IEditPage interface");
            }
            editPage = this as IEditPage;
        }
        protected override void OnPreLoad(EventArgs e)
        {

            base.OnPreLoad(e);

        }

        protected override void OnLoad(EventArgs e)
        {


            base.OnLoad(e);
        }


        protected bool IsUpdate
        {
            get
            {
                bool isUpdate = false;
                isUpdate = (Request.QueryString["Update"] == "true") ? true : false;
                return isUpdate;
            }
        }

        protected Pair[] Key
        {
            get
            {
                return SessionRepository.Instance[editPage.EntityName +  "_Key"] as Pair[];
            }
            set
            {
                SessionRepository.Instance[editPage.EntityName + "_Key"] = value;
            }
        }

        private BusinessObjectCollection GetBOCollectionAfterSettingKey()
        {
            BusinessObjectCollection col = editPage.BOCollection;
            foreach (Pair pair in Key)
            {
                col[pair.First.ToString()] = pair.Second;
            }
            return col;

        }

    }
}